/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.quiz;

import com.ivanbiz.injection.CallCtx;
import com.ivanbiz.kelas.Kelas;
import com.ivanbiz.kelas.KelasService;
import com.ivanbiz.matapelajaran.MataPelajaran;
import com.ivanbiz.matapelajaran.MataPelajaranService;
import com.ivanbiz.properties.SISBeanConstants;
import com.ivanbiz.properties.SISConstants;
import com.ivanbiz.properties.SISScreenConstants;
import com.ivanbiz.tools.IvanBiz;
import com.ivanbiz.user.User;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author Ivan
 */
public class QuizAction extends IvanBiz {

    ActionMessages actionMessages = new ActionMessages();

    public ActionForward quizList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        TitleQuizService titleQuizService = (TitleQuizService) CallCtx.getContext(request).getBean(SISBeanConstants.TITLE_QUIZ_SERVICE);
        MataPelajaranService mataPelajaranService = (MataPelajaranService) CallCtx.getContext(request).getBean(SISBeanConstants.MATA_PELAJARAN_SERVICE);
        KelasService kelasService = (KelasService) CallCtx.getContext(request).getBean(SISBeanConstants.KELAS_SERVICE);
        HttpSession session = request.getSession(true);
        try {            
            Kelas kelas = (Kelas)session.getAttribute(SISConstants.KELAS);
            List listQuiz = titleQuizService.getTitleQuizByKelas(kelas.getId());
            request.setAttribute(SISConstants.LIST_TITLE_QUIZ, listQuiz);
            List listMataPelajaran = mataPelajaranService.loadAll();
            request.setAttribute(SISConstants.LIST_MATA_PELAJARAN, listMataPelajaran);
            List listKelas = kelasService.loadAll();
            request.setAttribute(SISConstants.LIST_KELAS, listKelas);
            session.removeAttribute(SISConstants.TITLE_QUIZ);
        } catch (Exception ex) {
            Logger.getLogger(QuizAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.QUIZ_LIST);
    }

    public ActionForward quizListSiswa(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        TitleQuizService titleQuizService = (TitleQuizService) CallCtx.getContext(request).getBean(SISBeanConstants.TITLE_QUIZ_SERVICE);
        MataPelajaranService mataPelajaranService = (MataPelajaranService) CallCtx.getContext(request).getBean(SISBeanConstants.MATA_PELAJARAN_SERVICE);
        KelasService kelasService = (KelasService) CallCtx.getContext(request).getBean(SISBeanConstants.KELAS_SERVICE);
        HttpSession session = request.getSession(true);
        try {
            Kelas kelas = (Kelas)session.getAttribute(SISConstants.KELAS);
            List listQuiz = titleQuizService.getTitleQuizByKelas(kelas.getId());
            request.setAttribute(SISConstants.LIST_TITLE_QUIZ_SISWA, listQuiz);
            List listMataPelajaran = mataPelajaranService.loadAll();
            request.setAttribute(SISConstants.LIST_MATA_PELAJARAN, listMataPelajaran);
            List listKelas = kelasService.loadAll();
            request.setAttribute(SISConstants.LIST_KELAS, listKelas);
            session.removeAttribute(SISConstants.TITLE_QUIZ);
        } catch (Exception ex) {
            Logger.getLogger(QuizAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.QUIZ_LIST_SISWA);
    }

    public ActionForward quizAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        TitleQuizService titleQuizService = (TitleQuizService) CallCtx.getContext(request).getBean(SISBeanConstants.TITLE_QUIZ_SERVICE);
        MataPelajaranService mataPelajaranService = (MataPelajaranService) CallCtx.getContext(request).getBean(SISBeanConstants.MATA_PELAJARAN_SERVICE);
        QuizForm quizForm = (QuizForm) form;
        HttpSession session = request.getSession(true);
        TitleQuiz titleQuiz = new TitleQuiz();
        try {
            User user = (User) session.getAttribute(SISConstants.USER);
            MataPelajaran mataPelajaran = mataPelajaranService.loadById(quizForm.getMataPelajaran());
            titleQuiz.setNama(quizForm.getJudul());
            titleQuiz.setMataPelajaran(mataPelajaran);
            titleQuiz.setIsShare(String.valueOf(quizForm.getKelas()));
            titleQuiz.setOpen(SISConstants.NO);
            titleQuiz.setCreatedDt(new Date());
            titleQuiz.setCreatedBy(user.getNama());
            titleQuizService.saveTitleQuiz(titleQuiz);
            session.setAttribute(SISConstants.TITLE_QUIZ, titleQuiz);
            quizForm.reset(mapping, request);
        } catch (Exception ex) {
            Logger.getLogger(QuizAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.QUIZ_ADD);
    }

    public ActionForward quizSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        AnswerService answerService = (AnswerService) CallCtx.getContext(request).getBean(SISBeanConstants.ANSWER_SERVICE);
        HttpSession session = request.getSession(true);
        QuizForm quizForm = (QuizForm) form;
        try {
            User user = (User) session.getAttribute(SISConstants.USER);
            TitleQuiz titleQuiz = (TitleQuiz) session.getAttribute(SISConstants.TITLE_QUIZ);
            Question question = new Question();
            question.setDescription(quizForm.getPertanyaan());
            question.setTitleQuiz(titleQuiz);
            question.setCreatedBy(user.getNama());
            question.setCreatedDt(new Date());
            Answer answera = new Answer();
            answera.setDescription(quizForm.getJawabanA());
            answera.setScore(quizForm.getNilaiA());
            answera.setCreatedBy(user.getNama());
            answera.setCreatedDt(new Date());
            answera.setTitleId(String.valueOf(titleQuiz.getId()));
            Answer answerb = new Answer();
            answerb.setDescription(quizForm.getJawabanB());
            answerb.setScore(quizForm.getNilaiB());
            answerb.setCreatedBy(user.getNama());
            answerb.setCreatedDt(new Date());
            answerb.setTitleId(String.valueOf(titleQuiz.getId()));
            Answer answerc = new Answer();
            answerc.setDescription(quizForm.getJawabanC());
            answerc.setScore(quizForm.getNilaiC());
            answerc.setCreatedBy(user.getNama());
            answerc.setCreatedDt(new Date());
            answerc.setTitleId(String.valueOf(titleQuiz.getId()));
            Answer answerd = new Answer();
            answerd.setDescription(quizForm.getJawabanD());
            answerd.setScore(quizForm.getNilaiD());
            answerd.setCreatedBy(user.getNama());
            answerd.setCreatedDt(new Date());
            answerd.setTitleId(String.valueOf(titleQuiz.getId()));
            Answer answere = new Answer();
            answere.setDescription(quizForm.getJawabanE());
            answere.setScore(quizForm.getNilaiE());
            answere.setCreatedBy(user.getNama());
            answere.setCreatedDt(new Date());
            answere.setTitleId(String.valueOf(titleQuiz.getId()));
            List list = new ArrayList();
            list.add(answera);
            list.add(answerb);
            list.add(answerc);
            list.add(answerd);
            list.add(answere);
            answerService.saveQuestionAnswer(question, list);
            List listQuestionAndAnswer = answerService.getQuestionAndAnswerByTitle(titleQuiz.getId());
            request.setAttribute(SISConstants.LIST_QUESTION_AND_ANSWER, listQuestionAndAnswer);
            quizForm.reset(mapping, request);
        } catch (Exception ex) {
            Logger.getLogger(QuizAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.QUIZ_ADD);
    }

    public ActionForward quizAddUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        AnswerService answerService = (AnswerService) CallCtx.getContext(request).getBean(SISBeanConstants.ANSWER_SERVICE);
        QuizForm quizForm = (QuizForm) form;
        try {
            List listQuestionAndAnswer = answerService.getQuestionAndAnswerByQuestion(Long.parseLong(request.getParameter(SISConstants.PERTANYAAN_ID)));
            Question question = (Question) listQuestionAndAnswer.get(0);
            List listAnswer = (List) listQuestionAndAnswer.get(1);
            quizForm.setPertanyaanId(question.getId());
            quizForm.setPertanyaan(question.getDescription());
            Answer answerA = (Answer) listAnswer.get(0);
            quizForm.setJawabanIdA(answerA.getId());
            quizForm.setJawabanA(answerA.getDescription());
            quizForm.setNilaiA(answerA.getScore());
            Answer answerB = (Answer) listAnswer.get(1);
            quizForm.setJawabanIdB(answerB.getId());
            quizForm.setJawabanB(answerB.getDescription());
            quizForm.setNilaiB(answerB.getScore());
            Answer answerC = (Answer) listAnswer.get(2);
            quizForm.setJawabanIdC(answerC.getId());
            quizForm.setJawabanC(answerC.getDescription());
            quizForm.setNilaiC(answerC.getScore());
            Answer answerD = (Answer) listAnswer.get(3);
            quizForm.setJawabanIdD(answerD.getId());
            quizForm.setJawabanD(answerD.getDescription());
            quizForm.setNilaiD(answerD.getScore());
            Answer answerE = (Answer) listAnswer.get(4);
            quizForm.setJawabanIdE(answerE.getId());
            quizForm.setJawabanE(answerE.getDescription());
            quizForm.setNilaiE(answerE.getScore());
        } catch (Exception ex) {
            Logger.getLogger(QuizAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.QUIZ_ADD_UPDATE);
    }

    public ActionForward quizAddUpdateSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        AnswerService answerService = (AnswerService) CallCtx.getContext(request).getBean(SISBeanConstants.ANSWER_SERVICE);
        QuestionService questionService = (QuestionService) CallCtx.getContext(request).getBean(SISBeanConstants.QUESTION_SERVICE);
        HttpSession session = request.getSession(true);
        QuizForm quizForm = (QuizForm) form;
        try {
            User user = (User) session.getAttribute(SISConstants.USER);
            TitleQuiz titleQuiz = (TitleQuiz) session.getAttribute(SISConstants.TITLE_QUIZ);
            Question question = new Question();
            question.setId(quizForm.getPertanyaanId());
            question.setDescription(quizForm.getPertanyaan());
            question.setUpdateBy(user.getNama());
            question.setUpdateDt(new Date());
            Answer answera = new Answer();
            answera.setId(quizForm.getJawabanIdA());
            answera.setDescription(quizForm.getJawabanA());
            answera.setScore(quizForm.getNilaiA());
            answera.setUpdateBy(user.getNama());
            answera.setUpdateDt(new Date());
            Answer answerb = new Answer();
            answerb.setId(quizForm.getJawabanIdB());
            answerb.setDescription(quizForm.getJawabanB());
            answerb.setScore(quizForm.getNilaiB());
            answerb.setUpdateBy(user.getNama());
            answerb.setUpdateDt(new Date());
            Answer answerc = new Answer();
            answerc.setId(quizForm.getJawabanIdC());
            answerc.setDescription(quizForm.getJawabanC());
            answerc.setScore(quizForm.getNilaiC());
            answerc.setUpdateBy(user.getNama());
            answerc.setUpdateDt(new Date());
            Answer answerd = new Answer();
            answerd.setId(quizForm.getJawabanIdD());
            answerd.setDescription(quizForm.getJawabanD());
            answerd.setScore(quizForm.getNilaiD());
            answerd.setUpdateBy(user.getNama());
            answerd.setUpdateDt(new Date());
            Answer answere = new Answer();
            answere.setId(quizForm.getJawabanIdE());
            answere.setDescription(quizForm.getJawabanE());
            answere.setScore(quizForm.getNilaiE());
            answere.setUpdateBy(user.getNama());
            answere.setUpdateDt(new Date());
            List list = new ArrayList();
            list.add(answera);
            list.add(answerb);
            list.add(answerc);
            list.add(answerd);
            list.add(answere);
            answerService.updateQuestionAnswer(question, list);
            List listQuestionAndAnswer = answerService.getQuestionAndAnswerByTitle(titleQuiz.getId());
            request.setAttribute(SISConstants.LIST_QUESTION_AND_ANSWER, listQuestionAndAnswer);
            quizForm.reset(mapping, request);
        } catch (Exception ex) {
            Logger.getLogger(QuizAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.QUIZ_ADD);
    }

    public ActionForward quizAddDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        AnswerService answerService = (AnswerService) CallCtx.getContext(request).getBean(SISBeanConstants.ANSWER_SERVICE);
        HttpSession session = request.getSession(true);
        try {
            answerService.deleteByQuestion(Long.parseLong(request.getParameter(SISConstants.PERTANYAAN_ID)));
            TitleQuiz titleQuiz = (TitleQuiz) session.getAttribute(SISConstants.TITLE_QUIZ);
            List listQuestionAndAnswer = answerService.getQuestionAndAnswerByTitle(titleQuiz.getId());
            request.setAttribute(SISConstants.LIST_QUESTION_AND_ANSWER, listQuestionAndAnswer);
        } catch (Exception ex) {
            Logger.getLogger(QuizAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.QUIZ_ADD);
    }

    public ActionForward quizDetail(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        TitleQuizService titleQuizService = (TitleQuizService) CallCtx.getContext(request).getBean(SISBeanConstants.TITLE_QUIZ_SERVICE);
        AnswerService answerService = (AnswerService) CallCtx.getContext(request).getBean(SISBeanConstants.ANSWER_SERVICE);
        QuestionService questionService = (QuestionService) CallCtx.getContext(request).getBean(SISBeanConstants.QUESTION_SERVICE);
        QuizForm quizForm = (QuizForm) form;
        try {
            TitleQuiz titleQuiz = titleQuizService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            quizForm.setCount(titleQuiz.getCount());
            request.setAttribute(SISConstants.TITLE_QUIZ, titleQuiz);
            List listQuestionAndAnswer = answerService.getQuestionAndAnswerByTitle(titleQuiz.getId());
            request.setAttribute(SISConstants.LIST_QUESTION_AND_ANSWER, listQuestionAndAnswer);
        } catch (Exception ex) {
            Logger.getLogger(QuizAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.QUIZ_DETAIL);
    }

    public ActionForward quizEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        TitleQuizService titleQuizService = (TitleQuizService) CallCtx.getContext(request).getBean(SISBeanConstants.TITLE_QUIZ_SERVICE);
        AnswerService answerService = (AnswerService) CallCtx.getContext(request).getBean(SISBeanConstants.ANSWER_SERVICE);
        QuestionService questionService = (QuestionService) CallCtx.getContext(request).getBean(SISBeanConstants.QUESTION_SERVICE);
        HttpSession session = request.getSession(true);
        try {
            TitleQuiz titleQuiz = titleQuizService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            session.setAttribute(SISConstants.TITLE_QUIZ, titleQuiz);
            List listQuestionAndAnswer = answerService.getQuestionAndAnswerByTitle(titleQuiz.getId());
            request.setAttribute(SISConstants.LIST_QUESTION_AND_ANSWER, listQuestionAndAnswer);
        } catch (Exception ex) {
            Logger.getLogger(QuizAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.QUIZ_EDIT);
    }

    public ActionForward quizEditSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        AnswerService answerService = (AnswerService) CallCtx.getContext(request).getBean(SISBeanConstants.ANSWER_SERVICE);
        HttpSession session = request.getSession(true);
        QuizForm quizForm = (QuizForm) form;
        try {
            User user = (User) session.getAttribute(SISConstants.USER);
            TitleQuiz titleQuiz = (TitleQuiz) session.getAttribute(SISConstants.TITLE_QUIZ);
            Question question = new Question();
            question.setDescription(quizForm.getPertanyaan());
            question.setTitleQuiz(titleQuiz);
            question.setCreatedBy(user.getNama());
            question.setCreatedDt(new Date());
            Answer answera = new Answer();
            answera.setDescription(quizForm.getJawabanA());
            answera.setScore(quizForm.getNilaiA());
            answera.setCreatedBy(user.getNama());
            answera.setCreatedDt(new Date());
            answera.setTitleId(String.valueOf(titleQuiz.getId()));
            Answer answerb = new Answer();
            answerb.setDescription(quizForm.getJawabanB());
            answerb.setScore(quizForm.getNilaiB());
            answerb.setCreatedBy(user.getNama());
            answerb.setCreatedDt(new Date());
            answerb.setTitleId(String.valueOf(titleQuiz.getId()));
            Answer answerc = new Answer();
            answerc.setDescription(quizForm.getJawabanC());
            answerc.setScore(quizForm.getNilaiC());
            answerc.setCreatedBy(user.getNama());
            answerc.setCreatedDt(new Date());
            answerc.setTitleId(String.valueOf(titleQuiz.getId()));
            Answer answerd = new Answer();
            answerd.setDescription(quizForm.getJawabanD());
            answerd.setScore(quizForm.getNilaiD());
            answerd.setCreatedBy(user.getNama());
            answerd.setCreatedDt(new Date());
            answerd.setTitleId(String.valueOf(titleQuiz.getId()));
            Answer answere = new Answer();
            answere.setDescription(quizForm.getJawabanE());
            answere.setScore(quizForm.getNilaiE());
            answere.setCreatedBy(user.getNama());
            answere.setCreatedDt(new Date());
            answere.setTitleId(String.valueOf(titleQuiz.getId()));
            List list = new ArrayList();
            list.add(answera);
            list.add(answerb);
            list.add(answerc);
            list.add(answerd);
            list.add(answere);
            answerService.saveQuestionAnswer(question, list);
            List listQuestionAndAnswer = answerService.getQuestionAndAnswerByTitle(titleQuiz.getId());
            request.setAttribute(SISConstants.LIST_QUESTION_AND_ANSWER, listQuestionAndAnswer);
            quizForm.reset(mapping, request);
        } catch (Exception ex) {
            Logger.getLogger(QuizAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.QUIZ_EDIT);
    }

    public ActionForward quizEditUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        AnswerService answerService = (AnswerService) CallCtx.getContext(request).getBean(SISBeanConstants.ANSWER_SERVICE);
        QuizForm quizForm = (QuizForm) form;
        try {
            List listQuestionAndAnswer = answerService.getQuestionAndAnswerByQuestion(Long.parseLong(request.getParameter(SISConstants.PERTANYAAN_ID)));
            Question question = (Question) listQuestionAndAnswer.get(0);
            List listAnswer = (List) listQuestionAndAnswer.get(1);
            quizForm.setPertanyaanId(question.getId());
            quizForm.setPertanyaan(question.getDescription());
            Answer answerA = (Answer) listAnswer.get(0);
            quizForm.setJawabanIdA(answerA.getId());
            quizForm.setJawabanA(answerA.getDescription());
            quizForm.setNilaiA(answerA.getScore());
            Answer answerB = (Answer) listAnswer.get(1);
            quizForm.setJawabanIdB(answerB.getId());
            quizForm.setJawabanB(answerB.getDescription());
            quizForm.setNilaiB(answerB.getScore());
            Answer answerC = (Answer) listAnswer.get(2);
            quizForm.setJawabanIdC(answerC.getId());
            quizForm.setJawabanC(answerC.getDescription());
            quizForm.setNilaiC(answerC.getScore());
            Answer answerD = (Answer) listAnswer.get(3);
            quizForm.setJawabanIdD(answerD.getId());
            quizForm.setJawabanD(answerD.getDescription());
            quizForm.setNilaiD(answerD.getScore());
            Answer answerE = (Answer) listAnswer.get(4);
            quizForm.setJawabanIdE(answerE.getId());
            quizForm.setJawabanE(answerE.getDescription());
            quizForm.setNilaiE(answerE.getScore());
        } catch (Exception ex) {
            Logger.getLogger(QuizAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.QUIZ_EDIT_UPDATE);
    }

    public ActionForward quizEditUpdateSave(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        AnswerService answerService = (AnswerService) CallCtx.getContext(request).getBean(SISBeanConstants.ANSWER_SERVICE);
        QuestionService questionService = (QuestionService) CallCtx.getContext(request).getBean(SISBeanConstants.QUESTION_SERVICE);
        HttpSession session = request.getSession(true);
        QuizForm quizForm = (QuizForm) form;
        try {
            User user = (User) session.getAttribute(SISConstants.USER);
            TitleQuiz titleQuiz = (TitleQuiz) session.getAttribute(SISConstants.TITLE_QUIZ);
            Question question = new Question();
            question.setId(quizForm.getPertanyaanId());
            question.setDescription(quizForm.getPertanyaan());
            question.setUpdateBy(user.getNama());
            question.setUpdateDt(new Date());
            Answer answera = new Answer();
            answera.setId(quizForm.getJawabanIdA());
            answera.setDescription(quizForm.getJawabanA());
            answera.setScore(quizForm.getNilaiA());
            answera.setUpdateBy(user.getNama());
            answera.setUpdateDt(new Date());
            Answer answerb = new Answer();
            answerb.setId(quizForm.getJawabanIdB());
            answerb.setDescription(quizForm.getJawabanB());
            answerb.setScore(quizForm.getNilaiB());
            answerb.setUpdateBy(user.getNama());
            answerb.setUpdateDt(new Date());
            Answer answerc = new Answer();
            answerc.setId(quizForm.getJawabanIdC());
            answerc.setDescription(quizForm.getJawabanC());
            answerc.setScore(quizForm.getNilaiC());
            answerc.setUpdateBy(user.getNama());
            answerc.setUpdateDt(new Date());
            Answer answerd = new Answer();
            answerd.setId(quizForm.getJawabanIdD());
            answerd.setDescription(quizForm.getJawabanD());
            answerd.setScore(quizForm.getNilaiD());
            answerd.setUpdateBy(user.getNama());
            answerd.setUpdateDt(new Date());
            Answer answere = new Answer();
            answere.setId(quizForm.getJawabanIdE());
            answere.setDescription(quizForm.getJawabanE());
            answere.setScore(quizForm.getNilaiE());
            answere.setUpdateBy(user.getNama());
            answere.setUpdateDt(new Date());
            List list = new ArrayList();
            list.add(answera);
            list.add(answerb);
            list.add(answerc);
            list.add(answerd);
            list.add(answere);
            answerService.updateQuestionAnswer(question, list);
            List listQuestionAndAnswer = answerService.getQuestionAndAnswerByTitle(titleQuiz.getId());
            request.setAttribute(SISConstants.LIST_QUESTION_AND_ANSWER, listQuestionAndAnswer);
            quizForm.reset(mapping, request);
        } catch (Exception ex) {
            Logger.getLogger(QuizAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.QUIZ_EDIT);
    }

    public ActionForward quizEditDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        AnswerService answerService = (AnswerService) CallCtx.getContext(request).getBean(SISBeanConstants.ANSWER_SERVICE);
        HttpSession session = request.getSession(true);
        try {
            answerService.deleteByQuestion(Long.parseLong(request.getParameter(SISConstants.PERTANYAAN_ID)));
            TitleQuiz titleQuiz = (TitleQuiz) session.getAttribute(SISConstants.TITLE_QUIZ);
            List listQuestionAndAnswer = answerService.getQuestionAndAnswerByTitle(titleQuiz.getId());
            request.setAttribute(SISConstants.LIST_QUESTION_AND_ANSWER, listQuestionAndAnswer);
        } catch (Exception ex) {
            Logger.getLogger(QuizAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.QUIZ_EDIT);
    }

    public ActionForward quizDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        AnswerService answerService = (AnswerService) CallCtx.getContext(request).getBean(SISBeanConstants.ANSWER_SERVICE);
        try {
            answerService.deleteByTitle(Long.parseLong(request.getParameter(SISConstants.ID)));
        } catch (Exception ex) {
            Logger.getLogger(QuizAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.QUIZ);
    }

    public ActionForward quizOpenClose(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        TitleQuizService titleQuizService = (TitleQuizService) CallCtx.getContext(request).getBean(SISBeanConstants.TITLE_QUIZ_SERVICE);
        QuizForm quizForm = (QuizForm) form;
        HttpSession session = request.getSession(true);
        try {
            User user = (User) session.getAttribute(SISConstants.USER);
            TitleQuiz titleQuiz = titleQuizService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            if (request.getParameter(SISConstants.FLAG).equals(SISConstants.YES)) {
                titleQuiz.setOpen(SISConstants.NO);
                titleQuiz.setUpdateBy(user.getNama());
                titleQuiz.setUpdateDt(new Date());
                titleQuizService.updateTitleQuiz(titleQuiz);
            }
            if (request.getParameter(SISConstants.FLAG).equals(SISConstants.NO)) {
                titleQuiz.setOpen(SISConstants.YES);
                titleQuiz.setUpdateBy(user.getNama());
                titleQuiz.setUpdateDt(new Date());
                titleQuizService.updateTitleQuiz(titleQuiz);
            }
        } catch (Exception ex) {
            Logger.getLogger(QuizAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.QUIZ);
    }

    public ActionForward quizCount(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        TitleQuizService titleQuizService = (TitleQuizService) CallCtx.getContext(request).getBean(SISBeanConstants.TITLE_QUIZ_SERVICE);
        AnswerService answerService = (AnswerService) CallCtx.getContext(request).getBean(SISBeanConstants.ANSWER_SERVICE);
        QuizForm quizForm = (QuizForm) form;
        HttpSession session = request.getSession(true);
        try {
            User user = (User) session.getAttribute(SISConstants.USER);
            TitleQuiz titleQuiz = titleQuizService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            titleQuiz.setCount(quizForm.getCount());
            titleQuiz.setUpdateBy(user.getNama());
            titleQuiz.setUpdateDt(new Date());
            titleQuizService.updateTitleQuiz(titleQuiz);
            request.setAttribute(SISConstants.TITLE_QUIZ, titleQuiz);
            List listQuestionAndAnswer = answerService.getQuestionAndAnswerByTitle(titleQuiz.getId());
            request.setAttribute(SISConstants.LIST_QUESTION_AND_ANSWER, listQuestionAndAnswer);
            quizForm.reset(mapping, request);
        } catch (Exception ex) {
            Logger.getLogger(QuizAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.QUIZ_DETAIL);
    }
}
